package com.xy.api.constant;

import lombok.Getter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

public class UnitConstant {

    @Getter
    public enum CurrencyUnitEnum {
        USD("USD", "美元", "$"),
        CNY("CNY", "人民币", "¥");
        private String code;

        private String name;

        private String signal;

        CurrencyUnitEnum(String code, String name, String signal) {
            this.code = code;
            this.name = name;
            this.signal = signal;
        }
    }

    @Getter
    public enum WeightUnitEnum {
        KG("KG", "千克", "KG"),
        POUND("LB", "磅", "LB");
        private String code;

        private String name;

        private String signal;

        WeightUnitEnum(String code, String name, String signal) {
            this.code = code;
            this.name = name;
            this.signal = signal;
        }
    }

    @Getter
    public enum VolumeUnitEnum {
        CC("CC", "立方厘米", "CC"),
        CBM("CBM", "立方米", "CBM"),
        CBFT("CBFT", "立方英尺", "CBFT");
        private String code;

        private String name;

        private String signal;

        VolumeUnitEnum(String code, String name, String signal) {
            this.code = code;
            this.name = name;
            this.signal = signal;
        }
    }

    @Getter
    public enum LengthUnitEnum {
        CM("CM", "厘米", BigDecimal.valueOf(0.01)),
        M("M", "米", BigDecimal.valueOf(1.0)),
        FT("FT", "英尺", BigDecimal.valueOf(0.3048)),
        ;
        private String code;

        private String name;

        private BigDecimal factor;

        LengthUnitEnum(String code, String name, BigDecimal factor) {
            this.code = code;
            this.name = name;
            this.factor = factor;
        }

        // 将给定值从本单位转换为米
        public BigDecimal toMeters(BigDecimal value) {
            if (value == null) {
                throw new IllegalArgumentException("参数不能为空");
            }
            return value.multiply(this.factor).setScale(4, RoundingMode.HALF_UP);
        }

        // 将给定值从米转换为本单位
        public BigDecimal fromMeters(BigDecimal value) {
            if (value == null) {
                throw new IllegalArgumentException("参数不能为空");
            }
            return value.divide(this.factor, 4, RoundingMode.HALF_UP);
        }

        /**
         * 通过code获取长度单位枚举值
         *
         * @param code
         * @return
         */
        public static LengthUnitEnum getLengthUnitFromCode(String code) {
            for (LengthUnitEnum value : values()) {
                if (Objects.equals(value.getCode(), code)) {
                    return value;
                }
            }
            return null;
        }
    }
}
